/*
	Prototype is developed by Benato Denis.
	Prototype is a project started on 23/09/2013.
	Prototype is release under LGPLv3 license.
	If you use part of Prototype software on other
	projects, you must copy this comment into the source
	code of the other projects, and quote Benato Denis 
	in the credits of the other software.
*/

#include "Prototype.h"

#include "Stack.h"

Stack::Stack()
{
	struct StackCell* FirstStackCell = new struct StackCell;
	FirstStackCell->Next = (struct StackCell*)NULL;
	FirstStackCell->Previous = (struct StackCell*)NULL;
	FirstStackCell->Data = (unsigned char)0x00;
	this->CurrentStack = FirstStackCell;
}

Stack::~Stack()
{
	if (CurrentStack != (struct StackCell*)NULL)
	{
		while (this->CurrentStack->Previous != (struct StackCell*)NULL)
			this->CurrentStack = this->CurrentStack->Previous;
		while (this->CurrentStack->Next != (struct StackCell*)NULL)
		{
			struct StackCell* toDelete = this->CurrentStack;
			this->CurrentStack = this->CurrentStack->Next;
			if (toDelete != (struct StackCell*)NULL)
				delete toDelete;
		}
		delete this->CurrentStack;
	}
}

bool Stack::IsDataAvailable()
{
	if (this->CurrentStack != (struct StackCell*)NULL)
		return true;
	else
		return false;
}

void Stack::PUSH(unsigned char NewData)
{
	struct StackCell* NewStackCell = new struct StackCell;
	NewStackCell->Next = (struct StackCell*)NULL;
	NewStackCell->Previous = this->CurrentStack;
	NewStackCell->Data = NewData;
	this->CurrentStack = NewStackCell;
}

unsigned char Stack::POP()
{
	if (this->IsDataAvailable())
	{
		unsigned char DataToReturn = this->CurrentStack->Data;
		this->CurrentStack = this->CurrentStack->Previous;
		delete this->CurrentStack->Next;
		return DataToReturn;
	} else {
		return (unsigned char)0x00;
	}
}